var Skink; if (!Skink) Skink = {};

/***********
Skink.Translate
************/
if (!Skink.Translate) Skink.Translate = {};

Skink.Translate.baseApi    = "http://ajax.googleapis.com/ajax/services/language/";
Skink.Translate.apiVersion = "1.0";

Skink.Translate.init = function() {}

Skink.Translate.detect = function(text) {
    var result;
    Skink.dump("Text: " + text);

    var xhr = $.ajax({
        async:       false,
        timeout:     GOOGLE_LANG_TIMEOUT,
        complete:    Skink.Translate.onAjaxComplete,
        error:       Skink.Translate.onAjaxError,
        success: function(data) {
            Skink.dump('SUCCESS: ' + data);
            result = JSON.parse(data);
        },
        processData: false,
        type:        "GET",
        url:         Skink.Translate.baseApi + "detect",
        data:        "v=" + Skink.Translate.apiVersion + "&q=" + text
    });

    return result.responseData.language;
}

Skink.Translate.translate = function(text, sourceLang) {
    var result;
    Skink.dump("Text: " + text);

    var xhr = $.ajax({
        async:       false,
        timeout:     GOOGLE_LANG_TIMEOUT,
        complete:    Skink.Translate.onAjaxComplete,
        error:       Skink.Translate.onAjaxError,
        success: function(data) {
            Skink.dump('SUCCESS: ' + data);
            result = JSON.parse(data);
        },
        processData: false,
        type:        "GET",
        url:         Skink.Translate.baseApi + "translate",
        data:        "v=" + Skink.Translate.apiVersion + "&q=" + text +
                     "&langpair=" + sourceLang + "%7C" + Skink.Prefs.get('search-base-lang')
    });

    if (result.responseStatus == 400) {
        return result.responseDetails;
    } else {
        return result.responseData.translatedText;
    }
}

Skink.Translate.getLanguage = function(langCode) {
    air.trace('Language: ' + langCode);
    return TRANSLATION_LANGUAGES[langCode];
}

function loadOptions() {
    var langs;
    for (key in TRANSLATION_LANGUAGES) {
        langs += "<option value='" + TRANSLATION_LANGUAGES[key] + "'>" + key + "</option>";
    }
    document.getElementById('destLang').innerHTML = langs;
    document.getElementById('destLang').value = "en";
    langs = "<option value='detect'>Detect Language</option>" + langs;
    document.getElementById('srcLang').innerHTML = langs;
}

/**
 * Called by most of the Twitter ajax methods when complete
 * @param {Object} xhr the xhr object
 * @param {rstr} xhr a "response" string that indicates if the request was successful or not
 * @returns void
 */
Skink.Translate.onAjaxComplete = function(xhr, rstr) {
    if (xhr.readyState < 3) {
        Skink.dump("ERROR: Google Language API did not respond");
        Skink.UI.statusBar("ERROR: Google Language API did not respond")
        return;
    }
    Skink.dump("HEADERS:\n" + xhr.getAllResponseHeaders());
    Skink.dump("DATA:\n" + xhr.responseText);
    Skink.dump("COMPLETE: " + rstr);
};

/**
 * Called by most of the Twitter ajax methods on error
 * @param {Object} xhr the xhr object
 * @param {rstr} xhr a "response" string that indicates if the request was successful or not
 * @returns void
 */
Skink.Translate.onAjaxError = function(xhr, rstr) {
    Skink.dump("ERROR: " + rstr);
    if (xhr.readyState < 3) {
        Skink.dump("ERROR: Google Language API did not respond");
        Skink.UI.statusBar("ERROR: Google Language API did not respond")
    }
    //if (xhr.responseText) {
    //    try {
    //        var errorInfo = JSON.parse(xhr.responseText)
    //        if (errorInfo.error) {
    //            Skink.UI.statusBar('ERROR: "' + errorInfo.error + '"');
    //        } else {
    //            Skink.UI.statusBar('ERROR: Server returned invalid data');
    //        }
    //    } catch(e) {
    //        Skink.dump('Error parsing for JSON in error response');
    //        Skink.UI.statusBar('ERROR: Server returned invalid data');
    //    }
    //}
    Skink.UI.flashStatusBar();
};